package com.interheart.framecore.generator;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.generator.config.builder.ConfigBuilder;
import com.baomidou.mybatisplus.generator.engine.AbstractTemplateEngine;
import com.jfinal.template.Engine;
import com.jfinal.template.source.ClassPathSourceFactory;

import java.io.File;
import java.util.Map;

public class EnjoyTemplateEngine extends AbstractTemplateEngine {
    private static  Engine engine = new Engine();

    public EnjoyTemplateEngine() {
        //给enjoy添加支持boolean Is取值方式
        Engine.addFieldGetterToLast(new com.jfinal.template.expr.ast.FieldGetters.IsMethodFieldGetter());
    }

    @Override
    public EnjoyTemplateEngine init(ConfigBuilder configBuilder) {
        super.init(configBuilder);
        engine.setSourceFactory(new ClassPathSourceFactory());
        engine.setDevMode(true);
        engine.addSharedObject("StrUtil", new StrUtil());
        return this;
    }

    @Override
    public void writer(Map<String, Object> map, String templatePath, String outputFile) throws Exception {
       try {
           engine.getTemplate(templatePath).render(map, new File(outputFile));
       }catch (Exception e){
           logger.error("生成出错：==>" + e.getMessage());
       }

    }

    @Override
    public String templateFilePath(String filePath) {
            return filePath;
    }

}
